package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("TasteController")
public class TasteController extends CmiEmaController {

	@RequestMapping("/listTastes")
	public ModelAndView listTastes(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = tastePaginationService.updatePagination(request, pagination);
		Set<Taste> tastes = tastePaginationService.getPaginationTastes(pagination);

		mav.addObject("tastes", tastes);
		mav.addObject("entityName", "taste");
		mav.addObject("pagination", pagination);
		mav.setViewName("taste/listTastes.jsp");
		return mav;
	}

	public String listTastes() {
		return "redirect:/listTastes";
	}

	@RequestMapping("/viewTaste")
	public ModelAndView viewTaste(@RequestParam Integer tasteId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("taste", tasteDAO.findTasteByPrimaryKey(tasteId));
		mav.setViewName("taste/viewTaste.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteTastes")
	public ModelAndView confirmDeleteTastes(
			@RequestParam(value = "deleteTastesIds", required = true) Set<Integer> deleteTastesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Taste> deleteTastes = tasteDAO.findTastesByIds(deleteTastesIds);
		Set<Taste> referencedTastes = tasteService.loadReferenceTastes(deleteTastes);
		boolean hasReferenced;
		if (referencedTastes.size() == 0) {
			mav.addObject("deleteEntitys", deleteTastes);
			hasReferenced = false;
		} else {
			deleteTastes.removeAll(referencedTastes);
			mav.addObject("referencedEntitys", referencedTastes);
			mav.addObject("deleteEntitys", deleteTastes);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Taste");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listTastes");
		return mav;
	}

	@RequestMapping("/deleteTaste")
	public String deleteTaste(@RequestParam Integer tasteId) {
		Taste taste = tasteDAO.findTasteByPrimaryKey(tasteId);
		tasteService.deleteTaste(taste);
		return "forward:/listTastes";
	}

	@RequestMapping("/confirmDeleteTaste")
	public ModelAndView confirmDeleteTaste(@RequestParam Integer tasteId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("taste", tasteDAO.findTasteByPrimaryKey(tasteId));

		mav.setViewName("taste/deleteTaste.jsp");

		return mav;
	}

	@RequestMapping("/deleteTastes")
	public String deleteTastes(@RequestParam(value = "deleteTastesId", required = true) Set<Integer> deleteTastesId) {
		tasteService.deleteTastes(deleteTastesId);
		return "forward:/listTastes";
	}

	@RequestMapping("/editTaste")
	public ModelAndView editTaste(@RequestParam Integer tasteId) {

		ModelAndView mav = new ModelAndView();

		Taste taste = tasteDAO.findTasteByPrimaryKey(tasteId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("taste", taste);

		// 把所有的引用taste对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(tasteService.loadTasteObjectReferencedSet(taste));

		// 将编辑页面taste添加到mav中
		mav.setViewName("taste/editTaste.jsp");
		return mav;
	}

	@RequestMapping("/newTaste")
	public ModelAndView newTaste() {
		ModelAndView mav = new ModelAndView();
		Taste taste = new Taste();

		mav.addObject("taste", taste);

		// 把所有的引用taste对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(tasteService.loadTasteObjectReferencedSet(taste));

		mav.addObject("newFlag", true);
		mav.setViewName("taste/editTaste.jsp");

		return mav;
	}

	@RequestMapping("/saveTaste")
	public ModelAndView saveTaste(@ModelAttribute Taste taste) {

		ModelAndView mav = new ModelAndView();

		Taste existingTaste = CmiHibernateProxyUtil.deproxy(tasteDAO.findTasteByPhysicalPrimaryKey(taste.getName()));

		// 新建记录
		if (taste.getId() == null) {
			if (taste.equals(existingTaste)) {

				mav.addAllObjects(tasteService.loadTasteClassReferencedSet(taste));
				// mav = editContinue(mav, taste);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("taste/editTaste.jsp");
			} else {
				tasteService.saveTaste(taste);
				mav.setViewName("forward:/listTastes");
			}
		}
		// 编辑记录
		else {
			Taste originTaste = CmiHibernateProxyUtil.deproxy(tasteDAO.findTasteByPrimaryKey(taste.getId()));
			if (!taste.equals(originTaste) && taste.equals(existingTaste)) {
				mav.addAllObjects(tasteService.loadTasteClassReferencedSet(taste));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("taste/editTaste.jsp");
			} else {
				tasteService.saveTaste(taste);
				mav.setViewName("forward:/listTastes");
			}
		}
		return mav;
	}

	@RequestMapping("/tasteController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
